//
//  ViewController.swift
//  TestPwp
//
//  Created by Denny on 2018/11/21.
//  Copyright © 2018 Denny. All rights reserved.
//

import Cocoa

class ViewController: NSViewController,NSTableViewDelegate,NSTableViewDataSource {

    @IBOutlet weak var scrollView: NSScrollView!
    @IBOutlet weak var backGroundView: NSView!
    @IBOutlet weak var tablevView: NSTableView!
    
    @IBOutlet weak var testLabel: TMLinkLabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        let label = TMLinkLabel.init(frame: NSRect.zero);
        testLabel.clickClosure = { [weak self] (sender) in
            
            
            
        }
        
        self.backGroundView.wantsLayer = true;
        self.backGroundView.layer?.backgroundColor = NSColor.red.cgColor;
        
//        self.scrollView.isHidden = true;
        
        let bezierPath = NSBezierPath();
        
        bezierPath.move(to: NSPoint.init(x: 0, y: 100));
        bezierPath.line(to: NSPoint.init(x: 200, y: 100));
        bezierPath.line(to: NSPoint.init(x: 200, y: 0));
        bezierPath.line(to: NSPoint.init(x: 0, y: 0));
//        bezierPath.close();
        
        let maskLayer = CAShapeLayer();
        
        maskLayer.path = bezierPath.cgPath;
        
        self.backGroundView.layer?.mask = maskLayer;

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    /// This is decide where show alert
    var wherePoing:NSPoint = NSPoint.zero;
    
    
    /// This is an **showPop** alert *info*
    private func showPopup(where point:NSPoint) {
        
        
    }
    
    /**
        This is test parameter
     - Parameter point: the first
     - Parameter inpoint: the second
 
    */
    private func showPopup2(where point:NSPoint,in inpoint:NSPoint) {
        
        
    }
    
    /**
        This is show pop up alert.
     
        # Title is alert #
        1. show where **point** how to *show*.
        2. show time.
     
        ````
     let fix = 1;
     let fix = 2;
     
        ````
     
     * Use it to show
     * Don't Use in background thread
     
     
    */
    
    private func showPopupAlert(where point:NSPoint) {
        
        
    }
    
    /**
     
        Custom Show type.
        ````
        case left
        case right
        ````
    */
    
    enum ShowType {
        
        /// Show left
        case left
        
        /// Show right
        case right
    }
    
    public func numberOfRows(in tableView: NSTableView) -> Int {
        
//        ShowType.left
        return 100;
    }
    
    
    /* This method is required for the "Cell Based" TableView, and is optional for the "View Based" TableView. If implemented in the latter case, the value will be set to the view at a given row/column if the view responds to -setObjectValue: (such as NSControl and NSTableCellView). Note that NSTableCellView does not actually display the objectValue, and its value is to be used for bindings. See NSTableCellView.h for more information.
     */
//    optional public func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any?
//
//
//    /* NOTE: This method is not called for the View Based TableView.
//     */
//    optional public func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int)
//
//
//    /* Sorting support
//     This is the indication that sorting needs to be done.  Typically the data source will sort its data, reload, and adjust selections.
//     */
//    optional public func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor])
    
    
    public func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        
        let textField = NSTextField.init(string: "11111");
        return textField;
    }
    
    
//    public func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
//
//
//    }

    
    @IBAction func addToVaultAction(_ sender: Any) {
        
        let alert = NSAlert.init();
        alert.messageText = "Add To Vault";
        alert.alertStyle = .informational;
        alert.addButton(withTitle: "OK").setAccessibilityFocused(true);
        alert.addButton(withTitle: "Cancel");
        
        let popButton = NSPopUpButton.init(title: "", target: nil, action: nil);
        popButton.addItem(withTitle: "1");
        popButton.addItem(withTitle: "2");
        popButton.image = nil;
//        popButton.pullsDown = true;
        popButton.frame = NSRect.init(x: 0, y: 0, width: 295, height: 50);
        
        
        alert.accessoryView = popButton;
        
        alert.beginSheetModal(for: self.view.window!) { (modalResponse) in
            
            if modalResponse == NSApplication.ModalResponse.alertFirstButtonReturn {
                
                
                
            }
            
        }
        
    }


}

public extension NSBezierPath {
    
    public var cgPath: CGPath {
        let path: CGMutablePath = CGMutablePath()
        var points = [NSPoint](repeating: NSPoint.zero, count: 3)
        for i in (0 ..< self.elementCount) {
            switch self.element(at: i, associatedPoints: &points) {
            case .moveTo:
                path.move(to: CGPoint(x: points[0].x, y: points[0].y))
            case .lineTo:
                path.addLine(to: CGPoint(x: points[0].x, y: points[0].y))
            case .curveTo:
                path.addCurve(to: CGPoint(x: points[2].x, y: points[2].y),
                              control1: CGPoint(x: points[0].x, y: points[0].y),
                              control2: CGPoint(x: points[1].x, y: points[1].y))
            case .closePath:
                path.closeSubpath()
            }
        }
        return path
    }

    
}
